<?php
/**
 * @version $Id: oonfig.xposters.php 001 2007-09-09 18:34:23Z userxp $
 * @package  XPosters
 *
 * XPosterS Primary Class File
 * @copyright Copyright (C) 2005-2007 JoomlaTune Projects. All rights reserved.
 * @license http://creativecommons.org/ Creative Common License
 * @developed by Alexey Brin aka UserXP
 * 
 * web: http://joomlatune.ru
 * email: userxp@joomlaportal.ru
 *
 **/

defined('_VALID_MOS') or defined('_JEXEC') or die('Direct Access to this location is not allowed.');

/*$jxlib = & JXLibrary::getInstance();
$lib = $jxlib->base.DS.'html'.DS.'element.php';
if( file_exists( $lib )) {
	include_once( $lib );
}*/

class JXHtmlSelect extends JXHtmlElement
{
	var $start_tag	= 'select';
	var $finish_tag	= 'select';
	var $indent		= "\n";
	
	function addOption( $value='', $text='', $selected=null )
	{
		$option = new JXHtmlOption( $value, $text, $selected );
		$this->addChild( $option );
	} //addOption
	
	function addOptions( $rows, $value_field='id', $text_field='text' )
	{
		if( is_array( $rows ) ) {
			foreach( $rows as $row ) {
				$option = new JXHtmlOption( $row->$value_field, $row->$text_field );
				$this->addChild( $option );
			}
		}
		elseif( is_object( $rows ) ) {
			foreach( $rows as $row ) {
				$option = new JXHtmlOption( $row[$value_field], $row[$text_field] );
				$this->addChild( $option );
			}
		}
	} //addOptions
	
	function build( $check_default=false, $save=true, $check_children=true )
	{
		return parent::build( $check_default, $save, $check_children );
	} //build
}

class JXHtmlOption extends JXHtmlElement
{
	var $start_tag	= 'option';
	var $finish_tag	= 'option';
	//var $indent		= "\n";
	
	function JXHtmlOption( $value='', $text='', $selected=false )
	{
		$this->__construct( $value, $text, $selected );
	}
	
	function __construct( $value='', $text='', $selected=false )
	{
		$this->setParameter( 'value', $value );
		if( $selected ) $this->setParameter( 'selected', 'selected' );
		$this->inner_text = strval( $text );
	}
	
	function get( $value='', $text='', $selected=false )
	{
		$opt = & JXHtmlOption::getInstance();
		$opt->clear();
		$opt->setParameter( 'value', $value );
		if( $selected ) $opt->setSelected();
		$opt->inner_text = strval( $text );
		return $opt->build( false, false, false );
	}
	
	function setSelected()
	{
		$this->setParameter( 'selected', 'selected' );
	} //setSelected
	
	function & getInstance()
	{
		static $instance;
		if( !isset( $instance ) ) {
			$instance = new JXHtmlOption();
		}
		return $instance;
	}
}

class JXHtmlOptions extends JXHtmlElement
{
	var $start_tag		= 'option';
	var $finish_tag		= 'option';
	var $key_field		= 'id';
	var $value_field	= 'name';
	var $selected		= null;
	var $indent			= "\n";

	function JXHtmlOptions( $data, $key='id', $value='name', $selected=null )
	{
		$this->__construct( $data, $key, $value, $selected );
	} //JXHtmlOptions
	
	function __construct( $data, $key='id', $value='name', $selected=null )
	{
		$this->setData( $data, $key, $value, $selected );
	} //__construct
	
	function setData( $data, $key='id', $value='name', $selected=null )
	{
		$this->children = array();
		$this->selected = array();
		
		$key	= (string) $key;
		$value	= (string) $value;
		
		$this->key_field	= $key;
		$this->value_field	= $value;
		
		if( count( $data ) > 0 )
		{
			if( is_array( $data ) ) 
			{
				$this->children = $data;
			}
			else if( is_object( $data ) )
			{
				foreach( $data as $d )
				{
					$this->children[$d->$key] = $d->$value;
					if( isset( $data->$key ) )
					{
						$this->selected[$d->$key] = 1;
					}
				}
			}
		}
		$this->setSelection( $selected );

	} //setData
	
	function setSelection( $selected )
	{
		if( is_array( $selected ) and count( $selected ) > 0 )
		{
			if( is_array( $selected ) ) 
			{
				$this->selected = $selected;
			}
			else if( is_object( $selected ) )
			{
				foreach( $selected as $d )
				{
					$this->selected[$d->$key] = 1;
				}
			}
		}
		elseif( is_string( $selected ) or is_int( $selected ) )
		{
			$this->selected[$selected] = 1;
		}
	} //setSelection
	
	function getData()
	{
		return $this->children;
	} //getData
	
	function build( $check_default=false, $save=false, $check_children=false )
	{
		$kf		= $this->key_field;
		$vf		= $this->value_field;
		$result = '';
		
		foreach( $this->children as $option )
		{
			$selected = isset( $this->selected[$key] ) ? true : false;
			
			if( is_array( $option ) )
			{
				$key	= $option[$kf];
				$value	= $option[$vf];
			}
			elseif( is_object( $option ) )
			{
				$key	= $option->$kf;
				$value	= $option->$vf;
			}
			
			$r = JXHtmlOption::get( $key, $value, $selected );
			$result .= $r;
		}
		if( $save ) $this->output = $result;
		return $result;
	} //build
	
} //JXHtmlOptions
?>